﻿Imports ccapps.persistent.CustomerCreate
Public Class Contract_Create
    Inherits System.Web.UI.Page

    Dim smc_id As String
    Dim sm_id As String
    Dim hlcode As String
    Dim dn_id As String
    Dim dn_num As String
    Dim dn_type As String
    Dim dirnum_npcode As String
    Dim i As Integer
    Dim pin_customer_id As Integer
   

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session("user") = "" Or Session("user") Is Nothing Then
            ' Response.Redirect("../../login.aspx")
            Session("message") = "!!Session Timeout!!,Tekrar Giriş Yapmalısınız"
            Response.Clear()
            Response.Status = "302 Object Moved"
            Response.RedirectLocation = "CX_Exception.aspx"
            Response.End()
        End If

        chck_INTERNATIONAL_CALL.Enabled = False
        chck_INTERNATIONAL_ROAMING.Enabled = False
       
        Label2.Text = Session("cust_type")

        If (Label2.Text = "bireysel") Then  ' burada eger müşteri bireysel olarak işlem yapıyorsa tarife secili gelip tarife kısmı hidden olur.
            Label2.Text = Session("tarife")
            Dim rpcode_from_bireysel As Integer = Convert.ToInt32(Label2.Text)
            RPServiceStatusBireysel(rpcode_from_bireysel)
            hid.Visible = False

        End If


        If (Session("durum") = "MevcutSME") Then    ' müşteri Mevcut Tabledan gelirse
            Label1.Text = Session("Mevcut_Customer_id")
            Dim rpcode_from_mevcut As Integer = Convert.ToInt32(Label1.Text) 'Buralar diğer sayfalarada eklenecek
            RPServiceStatus(rpcode_from_mevcut) '25.02.2013

        Else
            Label1.Text = Session("customer_id")   'müşteri bireyselden gelirse
        End If

        pin_customer_id = Convert.ToInt32(Label1.Text)

        Label1.Text = pin_customer_id


        Dim dtmusteribilgileri As DataTable = GetCustomerCode(Convert.ToInt32(Label1.Text))

        Dim dtmusteribilgileri2 As DataTable = GetCustomerInfo(Convert.ToInt32(Label1.Text))



        musteribilgileri.InnerHtml = "<span style='FONT-SIZE: 12px'>MÜŞTERİ BİLGİLERİ: <BR/></span>"

        musteribilgileri.InnerHtml += "<span style='FONT-SIZE: 12px'>CustCode: " + dtmusteribilgileri.Rows(0)("custcode").ToString() + "</span>" + "<div style='FONT-SIZE:12px'>CustFirstName-CustLastName:  "

        musteribilgileri.InnerHtml += dtmusteribilgileri2.Rows(0)("ccfname").ToString() + " - " + dtmusteribilgileri2.Rows(0)("cclname").ToString() + "</div>"








    End Sub


    Private Sub AktivasyonReason()

        DDL_RS_CODE.Items.Clear()
        Dim dt2 As DataTable = aktivasyonType()


        If dt2.Rows.Count > 0 Then
            For Each dr2 As DataRow In dt2.Rows
                DDL_RS_CODE.Items.Add(New ListItem(dr2("rs_desc").ToString(), dr2("RS_ID").ToString())) 'rs_desc ve rscode Databasedeki sql variablelarının isimleri' 
            Next
        End If
    End Sub



    Private Sub DealerSecim()

        Dim smid As Integer

        If (TB_SM_SERIALNUM.Text <> "") Then
            Dim dt2 As DataTable = GetMsisdn(TB_SM_SERIALNUM.Text.Trim())

            If dt2.Rows.Count > 0 Then
                For Each dr3 As DataRow In dt2.Rows

                    smid = Convert.ToInt32(dr3("SM_ID").ToString())
                Next


            End If



        End If
        'Dim dt3 As DataTable = DealerInfo(smid)

        'If dt3.Rows.Count > 0 Then
        '    For Each dr3 As DataRow In dt3.Rows
        '        ' Label8.Text = dr3("ccfname").ToString() & "  " & dr3("cclname").ToString() & "  " & dr3("CCCITY").ToString()



        '    Next
        'End If
    End Sub

    Private Sub TarifeSecim()

        DDL_RPCODE.Items.Clear()





        Dim dt3 As DataTable = GetTariffContract(pin_customer_id)

        If dt3.Rows.Count > 0 Then
            For Each dr3 As DataRow In dt3.Rows
                DDL_RPCODE.Items.Add(New ListItem(dr3("des").ToString(), dr3("tmcode").ToString()))

            Next
        End If
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        i = 1
        lblSMHata.Text = ""
        TB_MSISDN.Text = ""
        TB_NewDealer.Text = ""
        If (chckPrelinked.Checked = True) Then


            If (TB_SM_SERIALNUM.Text <> "") Then
                Dim dt3 As DataTable = GetMsisdn(TB_SM_SERIALNUM.Text)

                If dt3.Rows.Count = 0 Then
                    lblSMHata.Text = "Girmiş olduğunuz ICC-ID deactive durumdadır. Lütfen farklı bir ICC-ID deneyiniz"

                ElseIf dt3.Rows.Count > 0 Then
                    For Each dr3 As DataRow In dt3.Rows



                        If dr3("sm_status").ToString() = "d" Then
                            lblSMHata.Text = "Girmiş olduğunuz ICC-ID deactive durumdadır. Lütfen farklı bir ICC-ID deneyiniz"
                        End If

                        If dr3("sm_status").ToString() = "a" Then
                            lblSMHata.Text = "Girmiş olduğunuz ICC-ID zaten kullanılmaktadır. Lütfen farklı bir ICC-ID deneyiniz"

                        End If

                        If dr3("sm_status").ToString() = "p" Then
                            lblSMHata.Text = "Girmiş olduğunuz ICC-ID ye ait bir telefon numarası yoktur. Lütfen farklı bir ICC-ID deneyiniz"

                        End If

                        If dr3("sm_status").ToString() = "r" Then
                            TB_MSISDN.Text = dr3("dn_num").ToString().Substring(2, 10)
                            smc_id = dr3("SMC_ID").ToString() '/*smc_id*/
                            sm_id = dr3("SM_ID").ToString()
                            hlcode = dr3("hlcode").ToString()
                            dn_id = dr3("DN_ID").ToString()   '/*dn_id*/
                            dn_num = dr3("dn_num").ToString() '/*dirnum*/
                            dn_type = dr3("dn_type").ToString() '*dn_type*/
                            dirnum_npcode = dr3("DIRNUM_NPCODE").ToString() '/*npcode*/

                            lblSMHata.Text = ""

                        End If

                    Next


                End If



            End If


        End If



        If (chckPrelinked.Checked = False) Then

            If (TB_SM_SERIALNUM.Text <> "") Then
                Dim dt3 As DataTable = CheckSIM(TB_SM_SERIALNUM.Text.Trim())
                If dt3.Rows.Count = 0 Then
                    lblSMHata.Text = "Girmiş olduğunuz ICC-ID deactive durumdadır. Lütfen farklı bir ICC-ID deneyiniz"

                ElseIf dt3.Rows.Count > 0 Then
                    For Each dr3 As DataRow In dt3.Rows
                        If (dr3("sm_status").ToString().Trim() <> "r") Then
                            Dim gen As String = dr3("sm_status").ToString()

                            If dr3("sm_status").ToString() = "d" Then
                                lblSMHata.Text = "Girmiş olduğunuz ICC-ID deactive durumdadır. Lütfen farklı bir ICC-ID deneyiniz"

                            End If

                            If dr3("sm_status").ToString() = "a" Then
                                lblSMHata.Text = "Girmiş olduğunuz ICC-ID zaten kullanılmaktadır. Lütfen farklı bir ICC-ID deneyiniz"

                            End If



                            If dr3("sm_status").ToString() = "r" Then
                                lblSMHata.Text = ""

                            End If


                        End If



                    Next


                End If



            End If


        End If



        TarifeSecim()
        AktivasyonReason()

        'DealerSecim()
        RPServiceStatus()
    End Sub

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ErrorMessage.Text = ""
        Dim confirmValue As String = Request.Form("confirm_value")
        If confirmValue = "Yes" Then
            Try
                Dim sm As String = TB_SM_SERIALNUM.Text.Trim()
                Dim rpcode, cntrl As Integer
                If Session("cust_type") = "bireysel" Then
                    cntrl = Convert.ToInt32(Label2.Text)
                    rpcode = cntrl
                Else
                    rpcode = Convert.ToInt32(DDL_RPCODE.SelectedValue)
                End If

                Dim rscode As Integer = Convert.ToInt32(DDL_RS_CODE.SelectedValue)
                Dim MES As String
                Dim msisdn As String

                If chckPrelinked.Checked = True Then

                    msisdn = "90" + TB_MSISDN.Text.Trim()
                    Dim uluslararasiArama As String = "N"
                    Dim uluslararasiDolasim As String = "N"
                    Dim rehber As String = "N"
                    Dim sc_id As String
                    Dim dealer_custcode As String = TB_NewDealer.Text.Trim()
                    Dim hcode As Integer
                    Dim d_id As Integer
                    Dim d_num As String
                    Dim npcode As Integer
                    Dim d_type As Integer
                    Session("rpcode") = DDL_RPCODE.SelectedItem.ToString()
                    Session("rscode") = DDL_RS_CODE.SelectedItem.ToString()

                    If chck_INTERNATIONAL_CALL.Checked = True Then
                        uluslararasiArama = "Y"
                    End If
                    If chck_INTERNATIONAL_ROAMING.Checked = True Then
                        uluslararasiDolasim = "Y"
                    End If
                    If chck_REHBER.Checked = True Then
                        rehber = "Y"
                    End If


                    Dim dt3 As DataTable = GetMsisdn(TB_SM_SERIALNUM.Text.Trim())

                    If dt3.Rows.Count > 0 Then
                        For Each dr3 As DataRow In dt3.Rows
                            smc_id = dr3("SMC_ID").ToString().Trim() '/*smc_id*/
                            sm_id = dr3("SM_ID").ToString().Trim()
                            hlcode = dr3("hlcode").ToString().Trim()
                            dn_id = dr3("DN_ID").ToString().Trim()  '/*dn_id*/
                            dn_num = dr3("dn_num").ToString().Trim() '/*dirnum*/
                            dn_type = dr3("dn_type").ToString().Trim() '*dn_type*/
                            dirnum_npcode = dr3("DIRNUM_NPCODE").ToString().Trim() '/*npcode*/
                        Next
                    End If

                    Session("sim") = TB_SM_SERIALNUM.Text.Trim()
                    Session("msisdn") = dn_num

                    Dim SM_ID2 As Integer = Convert.ToInt32(sm_id)
                    Dim DN_ID2 As Integer = Convert.ToInt32(dn_id)

                    Dim res As DataTable = CreateContract(pin_customer_id, rpcode, rscode, SM_ID2, DN_ID2, rehber, uluslararasiArama, uluslararasiDolasim, dealer_custcode, Session("user"), MES)

                    TB_MSISDN.Text = msisdn.Substring(2, 10)
                    If res.Rows.Count > 0 Then
                        For Each dr2 As DataRow In res.Rows
                            If dr2("co_id").ToString() <> -1 Then
                                Session("co_id") = dr2("co_id").ToString().Trim()
                                Session("customer_id") = pin_customer_id
                                Session("dealer_custcode") = dealer_custcode
                                Response.Redirect("DevirIslemSummary.aspx", False)
                                ErrorMessage.Text = ""
                            Else
                                ErrorMessage.Text = "Contract Create işlemi gerçekleştirilememiştir.Lütfen girmiş olduğunuz değerleri gözden geçiriniz"
                            End If
                        Next
                    Else
                        ErrorMessage.Text = "Contract Create işlemi gerçekleştirilememiştir."
                    End If
                End If  'chckPrelinked.Checked = True 

                If chckPrelinked.Checked = False Then


                    msisdn = "90" + TB_MSISDN.Text.Trim()
                    Dim uluslararasiArama As String = "N"
                    Dim uluslararasiDolasim As String = "N"
                    Dim rehber As String = "N"
                    Dim sc_id As String
                    Dim dealer_custcode As String = TB_NewDealer.Text.Trim()
                    Dim hcode As Integer
                    Dim d_id As Integer
                    Dim d_num As String
                    Dim npcode As Integer
                    Dim d_type As Integer
                    Session("rpcode") = DDL_RPCODE.SelectedItem.ToString()
                    Session("rscode") = DDL_RS_CODE.SelectedItem.ToString()

                    If chck_INTERNATIONAL_CALL.Checked = True Then
                        uluslararasiArama = "Y"
                    End If
                    If chck_INTERNATIONAL_ROAMING.Checked = True Then
                        uluslararasiDolasim = "Y"
                    End If
                    If chck_REHBER.Checked = True Then
                        rehber = "Y"
                    End If

                    Dim sm_status As String
                    Dim dt3 As DataTable = GetSIMInfo(TB_SM_SERIALNUM.Text.Trim())

                    If dt3.Rows.Count > 0 Then
                        For Each dr3 As DataRow In dt3.Rows
                            smc_id = dr3("SMC_ID").ToString().Trim() '/*smc_id*/
                            sm_id = dr3("SM_ID").ToString().Trim()
                            sm_status = dr3("SM_STATUS").ToString().Trim()
                        Next
                    End If

                    If sm_status = "a" Then
                        ErrorMessage.Text = "Girmiş olduğunuz SIM kart aktif durumdadır. Lütfen tekrar deneyiniz."
                    ElseIf sm_status = "d" Then
                        ErrorMessage.Text = "Girmiş olduğunuz SIM kart deaktif durumdadır. Lütfen tekrar deneyiniz."
                    End If

                    If sm_status = "r" Then

                        Dim dn_status As String
                        Dim dt As DataTable = GetMsisdnInfo(msisdn)
                        dn_id = dt.Rows(0)("DN_ID")
                        dn_status = dt.Rows(0)("DN_STATUS")
                        If dn_status = "n" Then
                            ErrorMessage.Text = "Girmis oldugunuz MSISDN sistemde mevcut degildir. Lutfen tekrar deneyiniz"
                        ElseIf dn_status = "p" Then
                            ErrorMessage.Text = "Girmis oldugunuz MSISDN Prelinked olarak sistemde mevcuttur. Lutfen tekrar deneyiniz"
                        Else
                            If dn_status = "a" Then
                                ErrorMessage.Text = "Girmiş olduğunuz MSISDN aktif durumdadır. Lütfen tekrar deneyiniz"
                            ElseIf dn_status = "d" Then
                                ErrorMessage.Text = "Girmiş olduğunuz MSISDN deaktif durumdadır. Lütfen tekrar deneyiniz"
                            End If

                            If dn_status = "r" Then
                                Session("sim") = TB_SM_SERIALNUM.Text.Trim()
                                Session("msisdn") = msisdn

                                Dim SM_ID2 As Integer = Convert.ToInt32(sm_id)
                                Dim DN_ID2 As Integer = Convert.ToInt32(dn_id)

                                Dim res As DataTable = CreateContract(pin_customer_id, rpcode, rscode, SM_ID2, DN_ID2, rehber, uluslararasiArama, uluslararasiDolasim, dealer_custcode, Session("user"), MES)

                                If res.Rows.Count > 0 Then
                                    For Each dr2 As DataRow In res.Rows
                                        If dr2("co_id").ToString() <> -1 Then
                                            Session("co_id") = dr2("co_id").ToString().Trim()
                                            Session("customer_id") = pin_customer_id
                                            Session("dealer_custcode") = dealer_custcode
                                            Response.Redirect("DevirIslemSummary.aspx", False)
                                            ErrorMessage.Text = ""
                                        Else
                                            ErrorMessage.Text = "Contract Create işlemi gerçekleştirilememiştir.Lütfen girmiş olduğunuz değerleri gözden geçiriniz"
                                        End If
                                    Next
                                Else
                                    ErrorMessage.Text = "Contract Create işlemi gerçekleştirilememiştir."
                                End If
                            End If 'dn_status=r 
                        End If

                    End If 'Sm-status=r

                End If



            Catch ex As Exception


                Session("message") = ex.Message
                Response.Clear()
                Response.Status = "302 Object Moved"
                Response.RedirectLocation = "CX_Exception.aspx"
                Response.End()




            End Try
        End If
        TB_MSISDN.Text = msisdn.Substring(2, 10)

    End Sub


   
   


    Private Sub RPServiceStatus(ByVal customer_id As Integer)
        Dim dtRpcode As DataTable = GetRPCodeInfo(customer_id) '25.02.2013
        Dim rpcode As Integer = Convert.ToInt32(dtRpcode.Rows(0)("tmcode").ToString()) '25.02.2013
        Dim dt2 As DataTable = GetRPServiceStatus(rpcode)
        If dt2.Rows.Count > 0 Then
            For Each dr2 As DataRow In dt2.Rows
                If dr2("sncode").ToString() = "40" Then

                    chck_INTERNATIONAL_CALL.Enabled = True
                End If

                If dr2("sncode").ToString() = "37" Then

                    chck_INTERNATIONAL_ROAMING.Enabled = True
                End If

            Next
        End If
    End Sub

    Private Sub RPServiceStatus()
        Dim dt2 As DataTable = GetRPServiceStatus(DDL_RPCODE.SelectedValue)
        Dim flag1 As Integer = 0
        Dim flag2 As Integer = 0
        If dt2.Rows.Count > 0 Then
            For Each dr2 As DataRow In dt2.Rows
                If dr2("sncode").ToString() = "40" Then
                    chck_INTERNATIONAL_CALL.Enabled = True
                    flag1 = 1
                Else
                    If flag1 <> 1 Then
                        chck_INTERNATIONAL_CALL.Enabled = False
                    End If
                End If
                If dr2("sncode").ToString() = "37" Then
                    chck_INTERNATIONAL_ROAMING.Enabled = True
                    flag2 = 1
                Else
                    If flag2 <> 1 Then
                        chck_INTERNATIONAL_ROAMING.Enabled = False
                    End If

                End If
            Next
        Else
            chck_INTERNATIONAL_CALL.Enabled = False
            chck_INTERNATIONAL_ROAMING.Enabled = False
        End If

    End Sub

    Private Sub RPServiceStatusBireysel(ByVal rpcode As Integer)
        Dim dt2 As DataTable = GetRPServiceStatus(rpcode)
        Dim flag1 As Integer = 0
        Dim flag2 As Integer = 0
        If dt2.Rows.Count > 0 Then
            For Each dr2 As DataRow In dt2.Rows
                If dr2("sncode").ToString() = "40" Then
                    chck_INTERNATIONAL_CALL.Enabled = True
                    flag1 = 1
                Else
                    If flag1 <> 1 Then
                        chck_INTERNATIONAL_CALL.Enabled = False
                    End If
                End If
                If dr2("sncode").ToString() = "37" Then
                    chck_INTERNATIONAL_ROAMING.Enabled = True
                    flag2 = 1
                Else
                    If flag2 <> 1 Then
                        chck_INTERNATIONAL_ROAMING.Enabled = False
                    End If

                End If
            Next
        Else
            chck_INTERNATIONAL_CALL.Enabled = False
            chck_INTERNATIONAL_ROAMING.Enabled = False
        End If

    End Sub

    Protected Sub chckPrelinked_CheckedChanged(sender As Object, e As EventArgs) Handles chckPrelinked.CheckedChanged
        If (chckPrelinked.Checked = True) Then
            If (TB_SM_SERIALNUM.Text <> "") Then
                Dim dt3 As DataTable = GetMsisdn(TB_SM_SERIALNUM.Text)

                If dt3.Rows.Count > 0 Then
                    For Each dr3 As DataRow In dt3.Rows

                        TB_MSISDN.Text = dr3("dn_num").ToString()

                        smc_id = dr3("SMC_ID").ToString() '/*smc_id*/
                        sm_id = dr3("SM_ID").ToString()
                        hlcode = dr3("hlcode").ToString()
                        dn_id = dr3("DN_ID").ToString()   '/*dn_id*/
                        dn_num = dr3("dn_num").ToString() '/*dirnum*/
                        dn_type = dr3("dn_type").ToString() '*dn_type*/
                        dirnum_npcode = dr3("DIRNUM_NPCODE").ToString() '/*npcode*/



                    Next


                End If



            End If
            TarifeSecim()
            AktivasyonReason()

            ' DealerSecim()

        End If



        If (chckPrelinked.Checked = True) Then
            TB_MSISDN.Text = ""

        End If
    End Sub

    Protected Sub DDL_RPCODE_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DDL_RPCODE.SelectedIndexChanged
        RPServiceStatus()
    End Sub

    Protected Sub TB_NewDealer_TextChanged(sender As Object, e As EventArgs) Handles TB_NewDealer.TextChanged
        '    Me.Page.RegisterStartupScript("javascript", "<script language=""javascript"">alert('Hatalı Dealer.');</script>")
        '    Dim dt As DataTable = DealerControl(TB_NewDealer.Text)
        '    'Label5.Text += dt.Rows(0)(0).ToString() 
        '    If dt.Rows.Count > 0 Then
        '        If Convert.ToInt32(dt.Rows(0)(0).ToString()) = 0 Then
        '            Me.Page.RegisterStartupScript("javascript", "<script language=""javascript"">alert('Hatalı Dealer.');</script>")
        '            TB_NewDealer.Text = ""
        '        End If
        '    Else
        '        Me.Page.RegisterStartupScript("javascript", "<script language=""javascript"">alert('Hatalı Dealer.');</script>")
        '        TB_NewDealer.Text = ""
        '    End If
        'End Sub

        Dim dt As DataTable = DealerControl(TB_NewDealer.Text)
        'Label5.Text += dt.Rows(0)(0).ToString() 
        If dt.Rows.Count > 0 Then
            If Convert.ToInt32(dt.Rows(0)(0).ToString()) = 0 Then
                Me.Page.RegisterStartupScript("javascript", "<script language=""javascript"">alert('Hatalı Dealer.');</script>")
                TB_NewDealer.Text = ""
            End If
        Else
            Me.Page.RegisterStartupScript("javascript", "<script language=""javascript"">alert('Hatalı Dealer.');</script>")
            TB_NewDealer.Text = ""
        End If
    End Sub
End Class


